package cloud.estate.community.service.impl;

import cloud.estate.commons.bean.Community;
import cloud.estate.commons.handler.exception.AddOneException;
import cloud.estate.commons.handler.exception.DealOneByIdException;
import cloud.estate.commons.handler.exception.FindOneException;
import cloud.estate.commons.handler.exception.UpdateOneException;
import cloud.estate.commons.mapper.CommunityMapper;
import cloud.estate.commons.util.JsonResult;
import cloud.estate.commons.util.ResultTool;
import cloud.estate.community.service.communityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements communityService {
    @Override
    public JsonResult addOneCommunity(Community community){
        try {
            boolean save = save(community);
            if (save){
                return  ResultTool.success("添加成功");
            }
            return ResultTool.fail(501,"添加失败");
        } catch (Exception e) {
            throw new AddOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult deleteOneCommunity(String id){
        try {
            boolean removeById = removeById(id);
            if (removeById){
                return  ResultTool.success("删除成功");
            }
            return ResultTool.fail(502,"删除失败");
        } catch (Exception e) {
            throw new DealOneByIdException(e.getMessage());
        }
    }
    @Override
    public JsonResult updateOneCommunity(Community community){
        try {
            boolean updateById = updateById(community);
            if (updateById){
                return  ResultTool.success("更新成功");
            }
            return ResultTool.fail(503,"更新失败");
        } catch (Exception e) {
            throw new UpdateOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult findOneCommunity(String id){
        try {
            Community community = getById(id);
            if (community != null){
                return  ResultTool.success(community);
            }
            return ResultTool.fail(504,"查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
    @Override
    public JsonResult findAllCommunity(){
        try {
            List<Community> list = list();
            if (list != null){
                return  ResultTool.success(list);
            }
            return  ResultTool.fail(505,"查询失败");
        } catch (Exception e) {
            throw new FindOneException(e.getMessage());
        }
    }
}
